<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\BaoxiaoCycleModel;
use app\admin\model\BaoxiaoModel;
use app\services\JsonService;
use app\services\UtilService as Util;
use app\validate\BaoxiaoValidate;
use cmf\controller\AdminBaseController;
use cmf\lib\Upload;
use think\Db;
use think\exception\ValidateException;

class BaoxiaoCycleController extends AdminBaseController
{
    function index(){
        return $this->fetch();
    }

    //获取报销数据
    public function data()
    {
        $page   = $this->request->param('page','1');
        $limit  = $this->request->param('limit','10');
        $model  = new BaoxiaoCycleModel();
        $res    = $model->getList($page,$limit);
        $count  = $model->count('id');
        if($res){
            JsonService::successlayui($count,$res,'数据列表');
        }else{
            JsonService::fail(BaoxiaoModel::getErrorInfo());
        }
    }

    //报销信息
    public function info()
    {
        $id     = $this->request->param('cycle_id','0');
        $info   = BaoxiaoCycleModel::getInfo($id);
        if($info===false){
            abort(500,BaoxiaoCycleModel::getErrorInfo());
        }
        $this->assign('info',$info);
        return $this->fetch('form');
    }

    //修改状态
    public function modify_status()
    {
        $cycle_id   = $this->request->param('cycle_id');
        $status     = $this->request->param('status');
        if($status==1){
            $count = BaoxiaoCycleModel::where('status',1)->count();
            if($count>0){
                JsonService::fail('同时只能存在一个进行中的报销周期！');
            }
        }
        $re   = BaoxiaoCycleModel::statusOperate($cycle_id,$status);
        if($re){
            JsonService::success('状态修改成功！');
        }else{
            JsonService::fail('您没有做任何修改');
        }
    }

    public function operate()
    {
        $data = Util::postMore([
            ['cycle_id', 0],
            'start_time',
            'end_time',
            'status',
            'name',
            'remark',
        ]);
        $baoxiaoModel = new BaoxiaoCycleModel();
        if($data['cycle_id']){
            $re = $baoxiaoModel->edit($data['cycle_id'],$data);
        }else{
            $re = $baoxiaoModel->create($data);
        }
        if($re){
            JsonService::success('操作成功');
        }else{
            JsonService::fail('操作失败');
        }
    }

}
